package com.cnc.fd.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cnc.bas.util.SpringBeanFactory;
import com.cnc.bas.util.StringUtil;
import com.cnc.fd.service.FdDao;

/**
 * 客户对应的可查询用户帮助类
 * 
 * @author Administrator
 * 
 */
public class QueryOpersHelper {

    private static boolean init = false;

    private QueryOpersHelper() {

    }

    private static QueryOpersHelper queryOpersHelper = new QueryOpersHelper();

    // 存放用户和可查询用户列表。以<oper, List<queryOpers>>的形式存放
    private Map<String, List<String>> queryOperMap = new HashMap<String, List<String>>();

    /**
     * 获取单实例
     * 
     * @return
     */
    public static QueryOpersHelper getInstance() {
        if (!init) {
            queryOpersHelper.init();
            init = true;
        }
        return queryOpersHelper;
    }

    /**
     * 初始化所有用户
     */
    private void init() {
        FdDao fdDao = (FdDao) SpringBeanFactory.getBean(FdDao.BEAN_ID);
        Map<String, String> dataMap = fdDao.queryAllQueryOpers();
        for (final String oper : dataMap.keySet()) {
            List<String> queryOpersList = new ArrayList<String>();
            // 添加自身
            queryOpersList.add(oper);
            List<String> list = StringUtil.strToList(dataMap.get(oper));
            if (list != null && !list.isEmpty()) {
                queryOpersList.addAll(list);
            }

            queryOperMap.put(oper, queryOpersList);
        }
    }

    /**
     * 刷新用户的可查询用户列表
     * 
     * @param oper
     */
    public void refresh(String oper) {
        FdDao fdDao = (FdDao) SpringBeanFactory.getBean(FdDao.BEAN_ID);
        String queryOpers = fdDao.getQueryOpersFromOper(oper);
        synchronized (queryOperMap) {
            List<String> queryOpersList = new ArrayList<String>();
            // 添加自身
            queryOpersList.add(oper);
            List<String> list = StringUtil.strToList(queryOpers);
            if (list != null && !list.isEmpty()) {
                queryOpersList.addAll(list);
            }

            queryOperMap.put(oper, queryOpersList);
        }
    }

    /**
     * 根据用户获取可查询用户列表
     * 
     * @param oper
     * @return
     */
    public List<String> getQueryOpersByOper(String oper) {
        synchronized (queryOperMap) {
            return queryOperMap.get(oper);
        }
    }

    /**
     * 判断用户是否有可查询的用户列表
     * 
     * @param oper
     * @return
     */
    public boolean hasQueryOpers(String oper) {
        if (queryOperMap.containsKey(oper)) {
            List<String> queryOperList = queryOperMap.get(oper);
            if (queryOperList != null && queryOperList.size() > 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断oper用户是否具有查询queryOper用户的权限
     * 
     * @param oper
     * @return
     */
    public boolean canQuery(String oper, String queryOper) {
        if (queryOperMap.containsKey(oper)) {
            List<String> queryOperList = queryOperMap.get(oper);
            if (queryOperList != null && queryOperList.contains(queryOper)) {
                return true;
            }
        }
        return false;
    }

}
